<?php

namespace App\Http\Controllers;

use App\month10model;
use Illuminate\Http\Request;

class month10controller extends Controller
{
    public function add(Request $request){
        $name = $request['name'];
        $pwd = $request['pwd'];
        $file = $request->file("img");

        //new orm
        $fsp = new month10model();
        $fsp['name']=$name;
        $fsp['pwd']=$pwd;
        $fsp->save();
        $id = $fsp->id;

        //设置图片上传类型
        $type = ['jpg','jpeg','png'];
        if (!in_array($file->getClientOriginalExtension(),$type)){
            dd("只能上传'jpg','jpeg','png'类型的图片");
        }
        //设置图片路径
        $path = __DIR__."/../../../public/imgs".intval($id/1000);
        $a = $file->getClientOriginalExtension();
        $filename = $id.".".$a;
        $file->move($path,$filename);
        $img = $path.'/'.$filename;
        $fsp['img']=$img;
        $a = $fsp->save();
        if ($a){
            return"<script>alert('添加成功');location.href='show'</script>";
        }else{
            return"<script>alert('添加失败');location.href='monthadd'</script>";
        }

//        $img = $path . DIRECTORY_SEPARATOR . $filename;
//        Image::make($request->img)->resize(50, 50, function ($constraint) {
//            $constraint->aspectRatio();
//        })->save($img);


    }
    public function show(){
        $data = month10model::li();
        return view("monthlist",['data'=>$data]);
    }

    public function get(Request $request){
        $id = $request['id'];
        $fsp = month10model::find($id);
        $img = $fsp['img'];
        return response()->file($img);
}

    public function gets(Request $request){
        $id = $request['id'];
        $fsp = month10model::find($id);
        $img = $fsp['img'];
        return response()->download($img,$id.".png");
    }






}

